<template>
	<view>
		<u-upload style="width: 180px;"  :header="headers" @afterRead="afterRead" max-count="1" :multiple="false">
			<u-button class="blue_btn" style="width: 180px;background: #2F6CBC;border: 1px solid #2F6CBC;"
				type="primary">上传简历(一键解析,自动填写)</u-button>
		</u-upload>
		<!-- <view  class="el-upload__tip">{{datas.tip}}</view> -->
	<!-- 	<el-upload class="upload-demo" :on-success="handleAvatarSuccess"  :action="action"
			:headers="headers" :before-upload="handleFileChange" :show-file-list="false">
			
		</el-upload> -->
	</view>
</template>
<script>
	import {
		URL_IP_HOST,
		URL_PREFIX
	} from '@/util/constant.js'
	import {
		parserResume
	} from "@/api/my.js"
	export default {
		data() {
			var userInfoData_ = uni.getStorageSync("userInfoData_")
			return {
				headers: {
					"Authorization": `Bearer ${userInfoData_ ? userInfoData_.access_token : ""}`,
					"clientid": userInfoData_ ? userInfoData_.client_id : "eeb9377d4b6debbc0a6e8756bbbbb4c7"
				},
				action: URL_IP_HOST + "/application/resume/parserResume",
				fileList:[],
			};
		},
		methods: {
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				let objs={}
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let obj=JSON.parse(result.data)
					console.log(obj);
					if(obj.code==200){
						objs = {
							type: obj.msg,
							bool: false,
							data: obj.data
						}
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							fileName:obj.fileName,
							url: obj.url,
							ossId:obj.ossId
						}))
						fileListLen++
					}else{
						objs = {
							type: obj.msg,
							bool: false,
							data: obj.data
						}
					}
				}
				this.$emit('uploadImageClick', objs)
			},
			uploadFilePromise(url) {
				let objs = {
					type: "解析中",
					bool: true,
					data: null
				}
				this.$emit('uploadImageClick', objs)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.action, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header:this.headers,
						formData: {
							user: 'test'
						},
						success: (res) => {
							
							setTimeout(() => {
								resolve(res)
							}, 1000)
						}
					});
				})
			},
			handleAvatarSuccess(res, file) {
				console.log("文件上传成功：", res, file);
				if (res.code == 200) {
					let obj = {
						type: "解析成功...",
						bool: false,
						data: res.data
					}
					this.$emit('uploadImageClick', obj)
					this.$message({
						message: `解析成功`,
						type: 'success',
						center: true,
						offset: 0.1
					});
				} else {
					let obj = {
						type: "解析失败...",
						bool: false,
					}
					this.$emit('uploadImageClick', obj)
					this.$message({
						message: `${res.msg}`,
						type: 'error',
						center: true,
						offset: 0.1
					});
				}

			},
			handleFileChange(rawFile) {
				console.log(rawFile);
				if (rawFile.type !== 'application/msword' &&rawFile.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' &&rawFile.type !== 'application/pdf' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/png') {
					this.$message({
						message: '格式有误! 仅支持jpg、png、doc、docx、pdf格式!',
						type: 'error',
						offset: 0.1
					});
					return false
				}
				// const file = event.raw;
				// const reader = new FileReader();
				// reader.onload = (e) => {
				// 	// 获取二进制数据流  
				// 	const binaryString = e.target.result;
				// 	// console.log(binaryString); // 打印二进制数据流  
				// 	// 在这里进行后续处理，例如发送到服务器等操作  
				let obj = {
					type: "正在解析...",
					bool: true,
				}
				this.$emit('uploadImageClick', obj)
				// 	let params = {
				// 		fileName: event.name,
				// 		data: binaryString
				// 	}
				// 	parserResume(params).then(res => {
				// 		let obj = {
				// 			type: "解析成功...",
				// 			bool: false,
				// 			data: {}
				// 		}
				// 		this.$emit('uploadImageClick', obj)
				// 	}).catch(res => {
				// 		let obj = {
				// 			type: "解析失败...",
				// 			bool: false,
				// 			data: {}
				// 		}
				// 		this.$emit('uploadImageClick', obj)

				// 	})
				// };
				// reader.readAsBinaryString(file); // 读取文件为二进制字符串  



			}
		}
	}
</script>
<style lang="scss" scoped>
	.blue_btn {
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
